package top.wardyan.wdev.dbdevassist.action.dic;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;

import top.wardyan.conf.Const;
import top.wardyan.control.WComposite;
import top.wardyan.control.WLabel;
import top.wardyan.control.WText;
//import top.wardyan.control.WText;
import top.wardyan.listener.WNumberOnlyKeyAdapter;
import top.wardyan.util.Msg;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbdevassist.action.base.BaseDetailEditShell;
import top.wardyan.wdev.dbdevassist.model.YTDicItem;
import top.wardyan.wdev.dbdevassist.service.IDetailService;

public class DicItemEditShell extends BaseDetailEditShell<DicItemEditShell, YTDicItem>{
	
	protected WComposite mainCom;
	WText txtSIndex;
	WText txtXValue;
	WText txtXName;
	WText txtXNote;
	
	
	
	public DicItemEditShell(int mID, int dID, IDetailService<YTDicItem> detailService) {
		super(mID, dID, detailService);
		if(self.isDisposed()) {
			return;
		}
		this.createContent();
		
		this.txtXValue.setFocus();
	}
	
	
	
	private void createContent() {
		mainCom = new WComposite(this, SWT.NONE);
		mainCom.setLayout(new FormLayout());
		//mainCom.setLayout(layout);
		FormData fd_mainCom = new FormData();
		fd_mainCom.left = new FormAttachment(0);
		fd_mainCom.top = new FormAttachment(topCom, 5);
		fd_mainCom.right = new FormAttachment(100, -1);
		fd_mainCom.bottom = new FormAttachment(100, -1);
		mainCom.setLayoutData(fd_mainCom);
		
		WComposite comSIndex = new WComposite(mainCom, SWT.NONE);;
		comSIndex.setLayout(new FormLayout());
		FormData fd_comSIndex = new FormData();
		fd_comSIndex.left = new FormAttachment(mainCom, 4);
		fd_comSIndex.top = new FormAttachment(mainCom, 0);
		//fd_comSIndex.right = new FormAttachment(comSIndex, 300);
		fd_comSIndex.right = new FormAttachment(25, 0);
		fd_comSIndex.bottom = new FormAttachment(comSIndex, 35);
		comSIndex.setLayoutData(fd_comSIndex);
		
		WLabel lblSIndex = new WLabel(comSIndex, SWT.BORDER, Const.FONT16, "序号", SWT.RIGHT);
		FormData fd_lblSIndex = new FormData();
		fd_lblSIndex.left = new FormAttachment(comSIndex, 0);
		fd_lblSIndex.top = new FormAttachment(comSIndex, 2);
		fd_lblSIndex.right = new FormAttachment(30);
		fd_lblSIndex.bottom = new FormAttachment(lblSIndex, 30);
		lblSIndex.setLayoutData(fd_lblSIndex);
		txtSIndex = new WText(comSIndex, SWT.BORDER, "", "序号");
		txtSIndex.setTextLimit(9);
		txtSIndex.addKeyListener(new WNumberOnlyKeyAdapter());
		FormData fd_txtSIndex = new FormData();
		fd_txtSIndex.left = new FormAttachment(lblSIndex, 4);
		fd_txtSIndex.top = new FormAttachment(comSIndex, 2);
		fd_txtSIndex.right = new FormAttachment(100, -8);
		fd_txtSIndex.bottom = new FormAttachment(txtSIndex, 30);
		txtSIndex.setLayoutData(fd_txtSIndex);
		//txtSIndex.setEditable(false);
		
		
		WComposite comXValue = new WComposite(mainCom, SWT.NONE);
		comXValue.setLayout(new FormLayout());
		FormData fd_comXValue = new FormData();
		fd_comXValue.left = new FormAttachment(comSIndex, 4);
		fd_comXValue.top = new FormAttachment(mainCom, 0);
		fd_comXValue.right = new FormAttachment(50, 0);
		fd_comXValue.bottom = new FormAttachment(comXValue, 35);
		comXValue.setLayoutData(fd_comXValue);
		WLabel lblXValue = new WLabel(comXValue, SWT.BORDER, Const.FONT16, "取值", SWT.RIGHT);
		FormData fd_lblXValue = new FormData();
		fd_lblXValue.left = new FormAttachment(comXValue, 0);
		fd_lblXValue.top = new FormAttachment(comXValue, 2);
		fd_lblXValue.right = new FormAttachment(30);
		fd_lblXValue.bottom = new FormAttachment(lblXValue, 30);
		lblXValue.setLayoutData(fd_lblXValue);
		txtXValue = new WText(comXValue, SWT.BORDER, "", "取值");
		txtXValue.setTextLimit(32);
		FormData fd_txtXValue = new FormData();
		fd_txtXValue.left = new FormAttachment(lblXValue, 4);
		fd_txtXValue.top = new FormAttachment(comXValue, 2);
		fd_txtXValue.right = new FormAttachment(100, -8);
		fd_txtXValue.bottom = new FormAttachment(txtXValue, 30);
		txtXValue.setLayoutData(fd_txtXValue);
		
		WComposite comXName = new WComposite(mainCom, SWT.NONE);
		comXName.setLayout(new FormLayout());
		FormData fd_comXName = new FormData();
		fd_comXName.left = new FormAttachment(comXValue, 4);
		fd_comXName.top = new FormAttachment(mainCom, 0);
		fd_comXName.right = new FormAttachment(75, 0);
		fd_comXName.bottom = new FormAttachment(comXName, 35);
		comXName.setLayoutData(fd_comXName);
		
		WLabel lblXName = new WLabel(comXName, SWT.BORDER, Const.FONT16, "名称", SWT.RIGHT);
		FormData fd_lblXName = new FormData();
		fd_lblXName.left = new FormAttachment(comXName, 0);
		fd_lblXName.top = new FormAttachment(comXName, 2);
		fd_lblXName.right = new FormAttachment(30);
		fd_lblXName.bottom = new FormAttachment(lblXName, 30);
		lblXName.setLayoutData(fd_lblXName);
		txtXName = new WText(comXName, SWT.BORDER, "", "名称");
		txtXName.setTextLimit(32);
		FormData fd_txtXName = new FormData();
		fd_txtXName.left = new FormAttachment(lblXName, 4);
		fd_txtXName.top = new FormAttachment(comXName, 2);
		fd_txtXName.right = new FormAttachment(100, -8);
		fd_txtXName.bottom = new FormAttachment(txtXName, 30);
		txtXName.setLayoutData(fd_txtXName);
		
		// 备注
		WComposite comXNote = new WComposite(mainCom, SWT.NONE);
		comXNote.setLayout(new FormLayout());
		FormData fd_comXNote = new FormData();
		fd_comXNote.left = new FormAttachment(comXName, 10);
		fd_comXNote.top = new FormAttachment(topCom, 2);
		fd_comXNote.right = new FormAttachment(100, -8);
		fd_comXNote.bottom = new FormAttachment(comXNote, 35);
		comXNote.setLayoutData(fd_comXNote);
		
		WLabel lblXNote = new WLabel(comXNote, SWT.BORDER, Const.FONT16, "备注", SWT.RIGHT);
		FormData fd_lblXNote = new FormData();
		fd_lblXNote.left = new FormAttachment(comXNote, 0);
		fd_lblXNote.top = new FormAttachment(comXNote, 2);
		fd_lblXNote.right = new FormAttachment(30);
		fd_lblXNote.bottom = new FormAttachment(lblXNote, 30);
		lblXNote.setLayoutData(fd_lblXNote);
		txtXNote = new WText(comXNote, SWT.BORDER, "", "备注");
		txtXNote.setTextLimit(256);
		FormData fd_txtXNote = new FormData();
		fd_txtXNote.left = new FormAttachment(lblXNote, 4);
		fd_txtXNote.top = new FormAttachment(comXNote, 2);
		fd_txtXNote.right = new FormAttachment(100, -8);
		fd_txtXNote.bottom = new FormAttachment(txtXNote, 30);
		txtXNote.setLayoutData(fd_txtXNote);
		self.pack();
		ShellUtil.center(self);
		this.texts.add(txtSIndex);
		this.texts.add(txtXValue);
		this.texts.add(txtXName);
		this.texts.add(txtXNote);
		
		txtSIndex.addKeyListener(new WNumberOnlyKeyAdapter());
		txtXValue.addKeyListener(new WNumberOnlyKeyAdapter());
		
		show();
		
		self.pack();
	}
	

	

	@Override
	protected boolean checkModelChange() {
		boolean rs = false;
		if(model != null) {
			rs = !model.getStr("sIndex").equals(txtSIndex.getText()) 
					|| !model.getStr("xName").equals(txtXName.getText())
					|| !model.getStr("xValue").equals(txtXValue.getText())
					|| !model.getStr("xNote").equals(txtXNote.getText());
		}else {
			rs = !txtXName.getText().equals("") && !txtXValue.getText().equals("");
		}
		return rs;
	}

	
	// 保存这个不好搞
	@Override
	protected boolean save() {
		if(checkInput()) {
			if(dID <= 0 || model == null) {
				model = new YTDicItem().setMID(this.mID);
			}
			model.setSIndex(Integer.valueOf(txtSIndex.getText()))
				.setXValue(Integer.valueOf(txtXValue.getText()))
				.setXName(txtXName.getText())
				.setXNote(txtXNote.getText());
			if(this.dID > 0) {
				return detailService.update(model);
			}else {
				return detailService.save(model);
			}
		}
		return false;
	}
	
	private boolean checkInput() {
		boolean rs = true;
		// sIndex, xName, xValue
		String msg = "";
		if(rs) {
			String sindex = txtSIndex.getText();
			if(sindex.length() == 0) {
				rs = false;
				msg = "序号异常";
			}
			try {
				Integer.valueOf(sindex);
			}catch(Exception e) {
				rs = false;
				msg = "序号异常";
			}
		}
		if(rs) {
			//YTDicItem d = new YTDicItem();
			String xvalue = txtXValue.getText();
			if(xvalue.length() == 0) {
				rs = false;
				msg = "取值异常";
			}
			try {
				Integer.valueOf(xvalue);
			}catch(Exception e) {
				rs = false;
				msg = "取值异常";
			}
		}
		if(rs) {
			if(txtXName.getText().length() == 0) {
				rs = false;
				msg = "名称异常";
			}
		}
		if(!rs) {
			Msg.error(self, msg);
		}
		return rs;
	}
	
	
	@Override
	protected void show() {
		txtSIndex.removeModifyListener(modifyListener);
		txtXName.removeModifyListener(modifyListener);
		txtXValue.removeModifyListener(modifyListener);
		txtXNote.removeModifyListener(modifyListener);
		
		if(model != null) {
			txtSIndex.setText(model.getStr("sIndex"));
			txtXName.setText(model.getStr("xName"));
			txtXValue.setText(model.getStr("xValue"));
			txtXNote.setText(model.getStr("xNote"));
		
		}else {
			YTDicItem item = detailService.findBySql("SELECT sIndex FROM " + YTDicItem.TABLENAME + " WHERE mID = ? ORDER BY sIndex DESC", self.mID);
			txtSIndex.setText(item != null && item.getSIndex() > 0 ? String.valueOf(item.getSIndex() + 1) : "1");
			txtXName.setText("");
			item = detailService.findBySql("SELECT xValue FROM " + YTDicItem.TABLENAME + " WHERE mID = ? ORDER BY xValue DESC", self.mID);
			txtXValue.setText(item != null && item.getXValue() > 0 ? String.valueOf(item.getXValue() + 1) : "1");
			txtXNote.setText("");
		
		}
		txtSIndex.removeModifyListener(modifyListener);
		txtXName.removeModifyListener(modifyListener);
		txtXValue.removeModifyListener(modifyListener);
		txtXNote.removeModifyListener(modifyListener);
	}


	

}
